<?php 
class CController {

	protected $m_objDatabase;
	protected $m_objLoginUser;
	protected $m_objLoginUserId;
	protected $m_objSmarty;
	protected $m_strAction;
	protected $m_objErrorMessage;
	protected $m_objData;
	
	public function __construct() {
		return true;
	}
	
	public function create() {
			
		$this->m_objDatabase = new CDatabase();
		$this->m_objDatabase->connectDatabase();
		$this->m_objDatabase->setDatabase();
		
		$this->m_objErrorMessage = new CErrorMessage();		
		if( true == isset( $_SESSION['success_messages'] ) ) {
			$this->m_objErrorMessage->setSuccessMessages( $_SESSION['success_messages'] );
			unset( $_SESSION['success_messages'] );
		}
		
		if( true == isset( $_SESSION['error_messages'] ) ) {
			$this->m_objErrorMessage->setErrorMessages( $_SESSION['error_messages'] );
			unset( $_SESSION['error_messages'] );
		}
		
		$this->m_objData = new CData();
		$this->m_objData->setData();
	}
	
	public function initialize() {
			
		$strAction = ( true == isset( $_REQUEST['action'] ) )?$_REQUEST['action']:NULL;
		$this->setAction( $strAction );
		$this->m_objLoginUserId = ( true == isset( $_SESSION[SITE_TITLE]['login_user_id'] ) )?$_SESSION[SITE_TITLE]['login_user_id']:NULL;
		
		if( true == isset( $_REQUEST['application'] ) && 'admin' == $_REQUEST['application'] && false == isset( $this->m_objLoginUserId ) && 'login' != $_REQUEST['module'] ) {
			header( 'location:' . SITE_PATH . '/admin/login' );
			exit;
		} 
		
		if( true == isset( $this->m_objLoginUserId ) ) {			
			$this->m_objLoginUser = CUsers::fetchUserById( $this->m_objLoginUserId, $this->m_objDatabase );
		}
	}
	
	public function execute() {
		return true;	
	}
	
	public function finalize() {
		return true;	
	}
	
	public function destroy() {
		$this->m_objDatabase->closeDatabase();
	}
	
	/**************************************
	************* SET FUNCTIONS ***********
	**************************************/
	
	public function setAction( $strAction ) {
		$this->m_strAction = $strAction;
	}
	
	/**************************************
	************* GET FUNCTIONS ***********
	**************************************/
	
	public function getAction() {
		return $this->m_strAction;		
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/	
	
	public function display() {
		require_once( SITE_ABSOLUTE_PATH . '/Liabraries/Smarty/Smarty.class.php' );
		
		$this->m_objSmarty = new Smarty;
		$this->m_objSmarty->compile_check = true;
		
		$this->m_objSmarty->assign( 'site_title', SITE_TITLE );
		$this->m_objSmarty->assign( 'site_path', SITE_PATH );
		$this->m_objSmarty->assign( 'site_css_path', SITE_PATH . '/Interfaces/common/css' );
		$this->m_objSmarty->assign( 'site_js_path', SITE_PATH . '/Interfaces/common/js' );
		$this->m_objSmarty->assign( 'site_images_path', SITE_PATH . '/Interfaces/common/images' );
		$this->m_objSmarty->assign( 'site_mount_path', SITE_PATH . '/Mount' );
		
		
		if( true == isset( $_REQUEST['application'] ) && 'admin' == $_REQUEST['application'] ) {
			$this->m_objSmarty->assign( 'admin_header1', TEMPLATE_DIR_PATH . '/admin/common/admin_header1.tpl' );
			$this->m_objSmarty->assign( 'admin_header2', TEMPLATE_DIR_PATH . '/admin/common/admin_header2.tpl' );
			$this->m_objSmarty->assign( 'admin_footer', TEMPLATE_DIR_PATH . '/admin/common/admin_footer.tpl' );
			$this->m_objSmarty->assign( 'admin_leftsidebar', TEMPLATE_DIR_PATH . '/admin/common/admin_leftsidebar.tpl' );
		
		} else {
			$this->m_objSmarty->assign( 'header1', TEMPLATE_DIR_PATH . '/common/header1.tpl' );
			$this->m_objSmarty->assign( 'header2', TEMPLATE_DIR_PATH . '/common/header2.tpl' );
			$this->m_objSmarty->assign( 'footer', TEMPLATE_DIR_PATH . '/common/footer.tpl' );	
			$this->m_objSmarty->assign( 'right_panel', TEMPLATE_DIR_PATH . '/common/right.tpl' );
				
		}

		$this->m_objSmarty->assign( 'error_messages', $this->m_objErrorMessage->getErrorMessages() );
		$this->m_objSmarty->assign( 'success_messages', $this->m_objErrorMessage->getSuccessMessages() );
		$this->m_objSmarty->assign( 'login_user', $this->m_objLoginUser );
	}

}
?>